package com.pig4cloud.pig.module.controller.attendance;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.attendance.AttendanceWorkShift;
import com.pig4cloud.pig.module.api.entity.property.Property;
import com.pig4cloud.pig.module.service.attendance.IAttendanceWorkShiftService;
import com.pig4cloud.pig.module.service.property.IPropertyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 工作班次Controller
 *
 * @author ruoyi
 * @date 2022-11-08
 */
@Validated
@Api(value = "工作班次控制器", tags = {"工作班次管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/attendance/shift")
public class AttendanceWorkShiftController {

    private final IAttendanceWorkShiftService workShiftService;
    private final IPropertyService propertyService;
    /**
     * 查询工作班次列表
     */
    @ApiOperation("查询工作班次列表")
    @PreAuthorize("@pms.hasPermission('module:shift:list')")
    @GetMapping("/list")
    public TableDataInfo<AttendanceWorkShift> list(@Validated AttendanceWorkShift attendanceWorkShift) {
        QueryWrapper<AttendanceWorkShift> qw = new QueryWrapper<>();
        TableDataInfo<AttendanceWorkShift> td = PageUtils.buildDataInfo(workShiftService.page(PageUtils.buildPage(),qw));
        List<AttendanceWorkShift> list = td.getData();
        for (AttendanceWorkShift list1:list){
            if (list1.getPropertyId()!=null){
                Property property=propertyService.findById(list1.getPropertyId());
                if (property!=null){
                    list1.setPropertyName(property.getPropertyName());
                }
            }
        }
        return td;
    }

    /**
     * 获取工作班次详细信息
     */
    @ApiOperation("获取工作班次详细信息")
    @PreAuthorize("@pms.hasPermission('module:shift:query')")
    @GetMapping("/{id}")
    public R<AttendanceWorkShift> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        AttendanceWorkShift attendanceWorkShift = workShiftService.getById(id);
        return R.ok(attendanceWorkShift);
    }

    /**
     * 获取工作班次详细信息
     */
    @ApiOperation("获取工作班次详细信息")
    @PreAuthorize("@pms.hasPermission('module:shift:query')")
    @GetMapping("/list/{id}")
    public TableDataInfo<AttendanceWorkShift> getTime(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        QueryWrapper<AttendanceWorkShift> qw = new QueryWrapper<>();
        if(id!=null){
            qw.eq("property_id",id);
        }
        TableDataInfo<AttendanceWorkShift> td = PageUtils.buildDataInfo(workShiftService.page(PageUtils.buildPage(),qw));
        List<AttendanceWorkShift> list = td.getData();
        return td;
    }
    /**
     * 新增工作班次
     */
    @ApiOperation("新增工作班次")
    @PreAuthorize("@pms.hasPermission('module:shift:add')")
    @SysLog("工作班次")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody AttendanceWorkShift attendanceWorkShift) {
        return R.okByRow(workShiftService.save(attendanceWorkShift) ? 1 : 0);
    }

    /**
     * 修改工作班次
     */
    @ApiOperation("修改工作班次")
    @PreAuthorize("@pms.hasPermission('module:shift:edit')")
    @SysLog("工作班次")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody AttendanceWorkShift attendanceWorkShift) {
        return R.okByRow(workShiftService.updateById(attendanceWorkShift) ? 1 : 0);
    }

    /**
     * 删除工作班次
     */
    @ApiOperation("删除工作班次")
    @PreAuthorize("@pms.hasPermission('module:shift:remove')")
    @SysLog("工作班次")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(workShiftService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
